<?php

class LoginActions extends sfActions
{

	public function executeSignin($request)
	{
		$user = $this->getUser();
		$this->form = new SigninForm();

		if ($request->isMethod('post'))
		{
			$this->form->bind($request->getParameter('signin'));
			if ($this->form->isValid())
			{
				$values = $this->form->getValues();
				$this->getUser()->signin($values['user']);

				$signinUrl = sfConfig::get('app_sf_guard_plugin_success_signin_url', $user->getReferer($request->getReferer()));

				return $this->redirect('' != $signinUrl ? $signinUrl : '@homepage');
			}
		}
		else
		{
			if ($user->isAuthenticated())
			{
				return $this->redirect('@homepage');
			}
			
			if ($request->isXmlHttpRequest())
			{
				$this->getResponse()->setHeaderOnly(true);
				$this->getResponse()->setStatusCode(401);

				return sfView::NONE;
			}

			$user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());

			$module = sfConfig::get('sf_login_module');
			if ($this->getModuleName() != $module)
			{
				return $this->redirect($module.'/'.sfConfig::get('sf_login_action'));
			}

			$this->getResponse()->setStatusCode(401);
		}
	}

	public function executeSignout($request)
	{
		$this->getUser()->signOut();

		$this->redirect('@homepage');
	}

	public function executeSecure($request)
	{
		$this->getResponse()->setStatusCode(403);
	}

}
